package cn.facetoken.homer;

import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.material.appbar.CollapsingToolbarLayout;

import static java.lang.String.format;

public class FaceCameraFragment extends Fragment {

    private Camera camera;
    private CameraPreview preview;
    private String cameraId;

    @Override
    public void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CollapsingToolbarLayout appBarLayout = requireActivity().findViewById(R.id.toolbar_layout);
        if (appBarLayout != null) {
            appBarLayout.setTitle(getResources().getString(R.string.demonstration_face_camera_title));
        }
    }

    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container,
                             @Nullable final Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.demonstration_face_camera, container, false);
        Log.i(getClass().getSimpleName(), format("onCreateView(): Root view %s is inflated.", rootView.getClass().getName()));
/*
        takenPhotoView = rootView.findViewById(R.id.taken_photo_view);
        //takenPhotoView.setOnClickListener(this);
        takenPhotoView.setOnCreateContextMenuListener(this);
        if (null == photoTaker) {
            photoTaker = new PhotoTaker(this, requireActivity(), takenPhotoView);
        }

        matchedPhotoView = rootView.findViewById(R.id.matched_photo_view);
        matchedNameTextView = rootView.findViewById(R.id.matched_name_text);
        similarityTextView = rootView.findViewById(R.id.similarity_text);
*/

        // Create an instance of Camera
        camera = getCamera(requireContext());
        Log.d(getClass().getSimpleName(), "onCreateView(): Succeeded to get the front Camera.");

        // Create our Preview view and set it as the content of our activity.
        preview = new CameraPreview(this, camera);
        Log.d(getClass().getSimpleName(), "onCreateView(): Succeeded to create a Camera Preview.");

        @NonNull final FrameLayout previewContainer = rootView.findViewById(R.id.camera_preview_container);
        Log.d(getClass().getSimpleName(), "onCreateView(): Succeeded to get a Frame Layout to contain a preview.");

        previewContainer.removeAllViews();
        previewContainer.addView(preview);
        Log.d(getClass().getSimpleName(), "onCreateView(): Succeeded to add a preview into the preview container.");

        return rootView;
    }

    /** A safe way to get an instance of the Camera object. */
    @Nullable
    public static Camera getCamera(final Context context){
        Camera camera = null;
        try {
            final int count = Camera.getNumberOfCameras();
            Log.i(FaceCameraFragment.class.getSimpleName(), String.format("getCamera(): Number of cameras is %d.", count));
            @NonNull Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            for (int i = 0; i < count; i++) {
                Camera.getCameraInfo(i, cameraInfo);
                Log.i(FaceCameraFragment.class.getSimpleName(), String.format("getCamera(): cameraInfo(%d): facing = %d, orientation = %d.", i, cameraInfo.facing, cameraInfo.orientation));
                if (Camera.CameraInfo.CAMERA_FACING_FRONT==cameraInfo.facing) {
                    camera = Camera.open(i);
                }
            }
        }
        catch (final Exception ignored){
            // Camera is not available (in use or does not exist)
            Log.e(FaceCameraFragment.class.getSimpleName(), "getCamera(): Failed to open front camera.", ignored);
        }

        if (null==camera) {
            Log.e(context.getClass().getSimpleName(), "getCamera(): Camera is not opened.");
        }
        return camera; // returns null if camera is unavailable
    }
}
